package com.topvision.myviewtest.mvp.demo2;

import com.topvision.myviewtest.mvp.bean.WeatherBean;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author lucy
 * @description 持有M层和V层
 * @date 2017-12-11 14:18
 */

public class RequestPresenter2 {

    private RequestModel2 mRequestModel;
    private RequestView2 mRequestView;

    public RequestPresenter2() {
        mRequestModel = new RequestModel2();
    }

    public void clickRequest(String cityId) {
        if (mRequestView != null) {
            mRequestView.requestLoading();
            mRequestModel.request(cityId, new Callback<WeatherBean>() {
                @Override
                public void onResponse(Call<WeatherBean> call, Response<WeatherBean> response) {
                    if (mRequestView != null) {
                        mRequestView.requestSuccess(response.body());
                    }
                }

                @Override
                public void onFailure(Call<WeatherBean> call, Throwable t) {
                    if (mRequestView != null) {
                        mRequestView.requestFailure(t.toString());
                    }
                }
            });
        }
    }

    /** 绑定View */
    public void attach(RequestView2 requestView) {
        mRequestView = requestView;
    }

    /** 解绑View */
    public void detach() {
        mRequestView = null;
    }

    /**
     * 取消网络请求
     */
    public void interrupHttp() {
        mRequestModel.interrupHttp();
    }

}
